package com.et.u1.vm.shop;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.et.u1.R;
import com.et.u1.databinding.FragmentShopBinding;
import com.et.u1.util.DataKt;
import com.et.u1.vm.base.BaseFragment;
import com.et.u1.vm.pay.PayFragment;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

public class ShopFragment extends BaseFragment<FragmentShopBinding, ShopViewModel> {

    private Timer timer;
    private int num;
    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                num--;
                Log.d("time", String.valueOf(num));
                if (num == 0) {
                    Objects.requireNonNull(getActivity()).getSupportFragmentManager().popBackStack();
                }
            } else if (msg.what == 2) {
                num = 120;
            }
        }
    };

    public static ShopFragment newInstance() {
        return new ShopFragment();
    }

    @NotNull
    @Override
    public FragmentShopBinding provideViewBinding(@NotNull LayoutInflater inflater, @org.jetbrains.annotations.Nullable ViewGroup container, @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return FragmentShopBinding.inflate(inflater, container, false);
    }

    @NotNull
    @Override
    public ShopViewModel provideViewModel() {
        return new ViewModelProvider(this).get(ShopViewModel.class);
    }

    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
    @Override
    public void initEvent() {

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this.getContext(), DataKt.getSize().x / 194);
        mainModel.bindGoodsAdapter(binding.recyclerview, gridLayoutManager, () -> {
            replaceFragment(PayFragment.newInstance());
            return null;
        });
        binding.recyclerview.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(getContext(), R.anim.item_anim));
        binding.recyclerview.setOnTouchListener((v, event) -> {
            mHandler.sendEmptyMessage(2);
            return false;
        });
    }

    private void timeClocks() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(1);
            }
        }, 1000, 1000);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        num = 120;
        timeClocks();
    }


    @Override
    public void onPause() {
        super.onPause();
        timer.cancel();
    }

    @Override
    public void onStop() {
        super.onStop();
    }
}